package com.party.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import com.party.domain.Reply;
/**
 * 
 * @author Caizhf
 * @date 2017年5月13日下午7:46:15
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: 回复</p>
 * 级联有ReplyMapper.xml文件
 */
public interface ReplyMapper {
	
	/**
	 * 插入回复的同时，需要插入是哪个用户回复的，还要插入
	 * 是对哪个版块进行回复（外键）
	 * @param reply
	 */
	@Insert("INSERT INTO reply(reply_content,reply_time,topic_id,user_id)"
			+ "VALUES(#{reply_content},#{reply_time},#{topic.tid},#{user.id})")
	@SelectKey(before=false,keyProperty="rid",resultType=Integer.class, statement = { "SELECT LAST_INSERT_ID()" })
	int add(Reply reply);
	
	/**
	 * 回复详情
	 * @param rid
	 * @return
	 */
	Reply findById(int rid);
	
	
	//通过id删除
	@Delete("DELETE FROM reply WHERE rid=#{rid}")
	int deleteById(int rid);
	
	/**
	 * 删除某个帖子下面的所有回复
	 * 批量删除操作
	 * @param topic_id
	 */
	int deleteAllByTopic(List<Reply> list);
	
	
	/**
	 * 根据板块id查询所有回复
	 * @param topic_id
	 * @return
	 */
	@Select("SELECT r.*,u.name,u.Id,u.icon,u.email FROM reply r,topic t,user u"
			+ " WHERE r.topic_id=t.tid AND r.user_id=u.Id"
			+ " AND t.tid=#{topic_id}")
	@ResultMap("com.party.mapper.ReplyMapper.ReplyBaseMapped")
	List<Reply> findByTopicId(int topic_id);
	
	
}
